import java.util.ArrayList;
import java.util.Arrays;

public class ThreadUtil {

    static void executeTask() throws InterruptedException {
        var q = new TaskQueue();
        var ts = new ArrayList<Thread>();
        for (int i = 0; i < 5; i++) {
            var t = new Thread(() -> {
                while (true) {
                    try {
                        String str = q.getTask();
                        System.out.println("execute task: " + str);
                    } catch (InterruptedException e) {
                        return;
                    }
                }
            });
            t.start();
            ts.add(t);
        }
        var add = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                String s = "t-" + Math.random();
                System.out.println("add task: " + s);
                q.addTask(s);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException _) {
                }
            }
        });
        add.start();
        add.join();
        Thread.sleep(100);
        for (var t : ts) {
            t.interrupt();
        }
    }

    static void testCounter() throws InterruptedException {
        var c1 = new Counter();
        var c2 = new Counter();
        new Thread(() -> {
            c1.inc(5);
        }).start();
        new Thread(() -> {
//            c1.del(5);
        }).start();
        Thread.sleep(100);
        System.out.println("c1 count: " + Arrays.toString(c1.get()));

        new Thread(() -> {
            c2.inc(50);
        }).start();
        new Thread(() -> {
//            c2.del(50);
        }).start();
        Thread.sleep(100);
        System.out.println("c2 count: " + Arrays.toString(c2.get()));
    }

    static void interruptThread() throws InterruptedException {
        Thread t = new MyThread();
        t.start();
        Thread.sleep(100);
        t.interrupt();
        t.join();
        System.out.println("end");
    }

    static void interruptRunningThread() throws InterruptedException {
        KThread t = new KThread();
        t.start();
        Thread.sleep(1);
        t.running = false; // 标志位置为false
    }

    static void joinSubThread() throws InterruptedException {
        System.out.println("main start");
        Thread thread = new Thread(() -> {
            System.out.println("thread start");
            System.out.println("thread end");
        });
        thread.start();
        thread.join();
        System.out.println("main end");
    }
}

class MyThread extends Thread {
    @Override
    public void run() {
        Thread hello = new HelloThread();
        hello.start();
        try {
            hello.join();
        } catch(InterruptedException e) {
            System.out.println("interrupted!");
        }
        hello.interrupt();
    }
}
class HelloThread extends Thread {
    @Override
    public void run() {
        int n = 0;
        while(!isInterrupted()) {
            n++;
            System.out.println(n + " hello ");
            try {
                Thread.sleep(100);
            } catch(InterruptedException e) {
                break;
            }
        }
    }
}

class KThread extends Thread {
    public volatile boolean running = true;
    @Override
    public void run() {
        int n = 0;
        while(running) {
            n++;
            System.out.println(n + " hello ");
        }
        System.out.println("end!");
    }
}
